package com.a.learning_keep.activity

import android.animation.ObjectAnimator
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.KeyEvent
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.a.learning_keep.myapp.MainActivity
import com.a.learning_keep.myapp.MyApplication
import com.a.learning_keep.R
import com.a.learning_keep.activity.login.Login
import com.a.learning_keep.base.token

class BootPage : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_boot_page)
        Handler().postDelayed({
            overridePendingTransition(R.anim.`in`, R.anim.out)
            if (MyApplication().myAppLoginState) {
                startActivity(Intent(this, MainActivity::class.java))
            } else {
                startActivity(Intent(this, Login::class.java))
            }

        }, 2000)
        Handler().postDelayed({
            hideView(findViewById<ImageView>(R.id.boot_img))
        }, 1500)

    }

    /**
     * 渐渐隐藏效果
     * View
     * */
    private fun hideView(view: View?) {
        val animator = ObjectAnimator.ofFloat(view, "alpha", 1.0f, 0.0f)
        animator.duration = 400
        animator.start()
    }

    /**
     * 限制中途退出
     * */
    var sta = false
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (event!!.action == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (!sta) {
                    Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show()
                    sta = true
                } else {
                    finishAffinity()
                    token = ""
                }
                Handler().postDelayed({
                    sta = false
                }, 1500)

            }
        }

        return true
    }
}